<?php

namespace App\Events\Reply;

use App\Consts\GlobalCounterConst;
use App\Models\SysReply;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class DelReplyEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    protected $channel = 'channel-event';

    /**
     * 回复基础信息
     * @var SysReply $reply SysReply::class
     */
    public $reply;

    /**
     * 记录【统计日志】所需数据
     * @var array $logCountLoadData [数据结构参看：LogCounterService()->getLoadDataByReply()]
     */
    public $logCountLoadData;

    public function __construct($replyInfo)
    {
        $this->reply = $replyInfo;

        $this->logCountLoadData = service()->LogCountService->getLoadDataByReply($this->reply, GlobalCounterConst::ACTION_TYPE_DEL_REPLY);
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel($this->channel);
    }

    /**
     * Execute the event.
     *
     * @return bool
     */
    public function handle()
    {
        Log::channel('event')->info(__CLASS__ . ' handle : ' . json_encode([
                'reply' => $this->reply,
                'log_count' => $this->logCountLoadData
            ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));

        return true;
    }
}
